package PRIO;

import java.util.Comparator;
import java.util.PriorityQueue;

class Person implements Comparable<Person>{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //自定义类的比较,需要重写comparAble接口里面的compareTo方法

    public Person() {
        super();
    }

    //比较方式1:1.大于小于比较,重写compareTo方法
    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }


}

//***比较方式2:自定义类比较器通过重写compare去进行比较
class NameComparator implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        return o1.name.compareTo(o2.name);
    }
}

//通过构造比较器将默认的小根堆变成大根堆
class Imp implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);//o2比o1大返回o2;
    }
}

public class Test2 {
    public static void main(String[] args) {
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);
        priorityQueue.offer(10);
        priorityQueue.offer(4);
        System.out.println("==");


    }
    public static void main1(String[] args) {
        Person person1 = new Person("zhangsan" , 10);
        Person person2 = new Person("lisi" , 8);
        System.out.println(person1.compareTo(person2));//比较大小,返回整数
        System.out.println(person1.equals(person2));//比较是否相同,返回布尔类型

        //构造器的比较调用
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(person1,person2));

    }
}
